package Yang.homework3;

import java.util.concurrent.locks.ReentrantLock;

public class SellTicketThread extends Thread {
    private static int ticket = 100;
    private ReentrantLock lock = new ReentrantLock();
    private static Demo1 de = new Demo1();
    @Override
    public  void run(){
        while (true) {
            synchronized (de) {
                if (ticket > 0) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "卖出第" + ticket + "张票！");
                    ticket--;
                } else {
                    System.out.println(Thread.currentThread().getName() + "余票不足！");
                    break;
                }
            }
        }
    }
}
class Demo1{}